/** @format */

const { Controller } = require('egg');

class UserController extends Controller {
    async index() {
        const { ctx, app } = this;
        console.log(ctx.request.header.token, 'ssssssssssss');
        try {
            let data = app.jwt.verify(ctx.request.header.token, app.config.jwt.secret);
            console.log(data.acount, 'telsss');
            // const post = await this.app.mysql.get('userinfo', {
            // 	acount: data.acount
            // });
            // console.log(post, 'post')
            ctx.body = {
                code: 200,
                // data: post
            };
        } catch (error) {
            console.error(error);
            ctx.body = {
                code: 201,
                data: '登录已失效',
            };
            // Expected output: ReferenceError: nonExistentFunction is not defined
            // (Note: the exact output may be browser-dependent)
        }
    }
    // 发送验证码

    async sendcode() {
        const { ctx } = this;
        let getroudnm = function () {
            let str = Math.round(Math.random() * 9);
            return str;
        };
        let mycode = `${getroudnm()}${getroudnm()}${getroudnm()}${getroudnm()}${getroudnm()}${getroudnm()}`;
        console.log(mycode, '验证码');
        ctx.session.mycode = mycode;
        this.ctx.session.maxAge = 60 * 1000;
        ctx.body = {
            code: 200,
            desp: `发送成功，${mycode}`,
        };
    }
    // 用户注册
    async regist() {
        const { ctx } = this;
        let clientdata = ctx.request.body;
        const post = await this.app.mysql.get('userinfo', { username: clientdata.username });
        if (post) {
            ctx.body = {
                code: 201,
                desp: '手机号码已被占用',
                // result,
            };
        } else {
            const result = await this.app.mysql.insert('userinfo', {
                username: clientdata.username,
                password: clientdata.password,
            });
            ctx.body = {
                code: 200,
                desp: '注册成功',
                result,
            };
        }
    }
    async getfrendimg() {
        const { ctx } = this;
        let clientdata = ctx.request.query;
        console.log(clientdata);
        const results = await this.app.mysql.select('images', {
            // 搜索 posts 表
            where: { username: clientdata.username }, // WHERE 条件
        });
        ctx.body = {
            code: 200,
            data: results,
            desc: '获取成功',
        };
    }

    async login() {
        const { ctx, app } = this;
        let clientdata = ctx.request.body;
        console.log(clientdata);
        const post = await this.app.mysql.get('userinfo', { username: clientdata.username });
        if (post) {
            if (clientdata.password != post.password) {
                ctx.body = {
                    code: 201,
                    desp: '密码不正确',
                };
            } else {
                let token = app.jwt.sign(
                    {
                        username: clientdata.username,
                        uid: post.id,
                    },
                    app.config.jwt.secret,
                    {
                        expiresIn: 60 * 60 * 24,
                    }
                );
                ctx.body = {
                    code: 200,
                    desp: '登录成功',
                    content: post,
                    token,
                };
            }
        } else {
            ctx.body = {
                code: -1,
                desp: '账号不存在',
            };
        }
    }
    async getUserInfo() {
        const { ctx, app } = this;
        console.log(ctx.request.header.token, 'ssssssssssss');
        try {
            let data = app.jwt.verify(ctx.request.header.token, app.config.jwt.secret);
            console.log(data, '我是用户');
            const post = await this.app.mysql.get('userinfo', {
                phone: data.phone,
            });
            // console.log(post, 'post')
            ctx.body = {
                code: 200,
                content: post,
            };
        } catch (error) {
            console.error(error);
            ctx.body = {
                code: 201,
                data: '登录已失效',
            };
            // Expected output: ReferenceError: nonExistentFunction is not defined
            // (Note: the exact output may be browser-dependent)
        }
    }
}

module.exports = UserController;
